package ru.susu.algebra.ranks;
/**
 * @author akargapolov
 * @since: 10.10.2011
 */
public class Integrals4Assymptotics
{
	private static double STEP = 10e-6;

	private static double N = 10000;
	private static double s = Math.PI * Math.sqrt(N - 1 / 24.0) / Math.sqrt(24.0);
	private static double A = -2 + STEP;
	private static double B = 2 - STEP;

	public static void main(String[] args)
	{
		System.out.println("N - " + N);
		System.out.println("s - " + s);

		System.out.println("sqrt(pi / s) - " + Math.sqrt(Math.PI / s));

		System.out.println("e^(-s*x*x) - " + calculate(A, B, new IFunction()
		{
			@Override
			public double calc(double x)
			{
				return Math.pow(Math.E, -s * x * x);
			}
		}));

		System.out.println("e^(-s*x*x) * sqrt(1 - x*x/4) - " + calculate(A, B, new IFunction()
		{
			@Override
			public double calc(double x)
			{
				return Math.pow(Math.E, -s * x * x) * Math.sqrt(1 - x * x / 4.0);
			}
		}));

		System.out.println("e^(-s*x*x) / sqrt(1 - x*x/4) - " + calculate(A, B, new IFunction()
		{
			@Override
			public double calc(double x)
			{
				return Math.pow(Math.E, -s * x * x) / Math.sqrt(1 - x * x / 4.0);
			}
		}));

		System.out.println("e^(-s*x*x) * (1 - x*x/2) / sqrt(1 - x*x/4) - " + calculate(A, B, new IFunction()
		{
			@Override
			public double calc(double x)
			{
				return Math.pow(Math.E, -s * x * x) * (1 - x * x / 2.0) / Math.sqrt(1 - x * x / 4.0);
			}
		}));

		System.out.println("e^(-s*x*x) * (1 - x*x/2) / sqrt(1 - x*x/4) - e^(-s*x*x) - " + calculate(A, B, new IFunction()
		{
			@Override
			public double calc(double x)
			{
				return Math.pow(Math.E, -s * x * x) * (1 - x * x / 2.0 - Math.sqrt(1 - x * x / 4.0)) / Math.sqrt(1 - x * x / 4.0);
			}
		}));
	}

	/**
	 * Вычисление интеграла по формуле симпсона
	 */
	private static double calculate(double a, double b, IFunction function)
	{
		double result = 0.0;
		for (double x = a; x + STEP <= b; x += STEP)
		{
			result += STEP / 6.0 * (function.calc(x) + 4 * function.calc(x + STEP / 2.0) + function.calc(x + STEP));
		}
		return result;
	}

	static interface IFunction
	{
		double calc(double x);
	}

}
